import Vue from 'vue'
import VueRouter from 'vue-router'
Vue.use(VueRouter)

import Layout from '@/layouts'

// 解决 Vue 重复点击相同路由,出现报错
const VueRouterPush = VueRouter.prototype.push
VueRouter.prototype.push = function push (to) {
  return VueRouterPush.call(this, to).catch(err => err)

}
//所有权限通用路由表 
//如首页和登录页和一些不用权限的公用页面
export const constantRoutes = [
  // {
  //   path: '/',
  //   redirect: '/login',
    
  // },
  {
    path: '/login',
    name: 'Login',
    component: () => import('../views/Login.vue')
  },
  {
    path: '/',
    component: Layout,
    redirect: '/index',
    children: [
      {
        path: 'index',
        component: () => import('@/layouts/index'),
        name: 'Index',
        meta: { title: '首页', icon: 'home', affix: true }
      }
    ]
  },
  {
    path: '/personnelManagement',
    component: Layout,
    // redirect: 'noRedirect',
    name: 'PersonnelManagement',
    meta: { title: '人员', icon: 'user', permissions: ['admin'] },
    children: [
      {
        path: '/userManagement',
        name: 'UserManagement',
        component: () => import('@/views/personnelManagement/userManagement/index'),
        meta: { title: '用户管理' },
      },
      {
        path: '/roleManagement',
        name: 'RoleManagement',
        component: () => import('@/views/personnelManagement/roleManagement/index'),
        meta: { title: '角色管理' },
      },
      {
        path: '/menuManagement',
        name: 'MenuManagement',
        component: () => import('@/views/personnelManagement/menuManagement/index'),
        meta: { title: '菜单管理', badge: 'New' },
      },
    ],
  },
]

//实例化vue的时候只挂载constantRoutes
const router = new VueRouter({
  mode: 'history',
  routes: constantRoutes
})

export default router
/**
 * 登录权限具体实现
 * 1.创建vue实例的时候将vue-router挂载，但这个时候vue-router挂载一些登录或者不用权限的公用的页面。
   2.当用户登录后，获取用role，将role和路由表每个页面的需要的权限作比较，生成最终用户可访问的路由表。
   3.调用router.addRoutes(store.getters.addRouters)添加用户可访问的路由。
   4.使用vuex管理路由表，根据vuex中可访问的路由渲染侧边栏组件。
 */

//异步挂载的路由
//动态需要根据权限加载的路由表 
// export const asyncRoutes = [
//   {
//     path: '/',
//     component: Layout,
//     redirect: '/index',
//     children: [
//       {
//         path: 'index',
//         name: 'Index',
//         component: () => import('@/views/layout/index'),
//         meta: {
//           title: '首页',
//           icon: 'home',
//           roles: ['admin', 'editor'] // you can set roles in root nav
//         }, 
//       },
//     ],
//   },
//   { path: '*', redirect: '/404', hidden: true }
// ];


